package com.stack.knowyouservice.service.impl;

import com.stack.knowyoubase.constant.GlobalConst;
import com.stack.knowyoubase.util.AliYunOssUtil;
import com.stack.knowyoubean.bean.FileType;
import com.stack.knowyouservice.service.LabReportService;
import org.apache.commons.io.FileUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.*;

/**
 * 实验报告服务层
 *
 * @author stack
 */
@Service
@Scope("singleton")
public class LabReportServiceImpl implements LabReportService {

    @Resource
    private AliYunOssUtil aliYunOssUtil;

    @Override
    public boolean reportUpload(MultipartFile file, Integer sid, Authentication authentication) {
        File newFile = new File(Objects.requireNonNull(file.getOriginalFilename()));
        String fileUrl;
        try {
            FileUtils.copyInputStreamToFile(file.getInputStream(), newFile);
            String fileName = sid + GlobalConst.EXPREPORT_FILE + authentication.getName() + "/" + file.getOriginalFilename();
            fileUrl = aliYunOssUtil.upLoadFile(newFile, fileName);
        } catch (Exception e) {
            return false;
        } finally {
            if (newFile.exists()) {
                newFile.delete();
            }
        }
        return !StringUtils.isEmpty(fileUrl);
    }

    @Override
    public Map<String, Object> reportInfo(Integer sid, String filePath, Integer page, Integer limit, Authentication authentication) {
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        String role = "";
        for (GrantedAuthority authority : authorities) {
            //获取角色信息
            role = authority.getAuthority();
        }
        String fileBaseUrl = "";
        if (GlobalConst.STUDENT_ROLE.equals(role)) {
            //只能精确到学号
            fileBaseUrl = authentication.getName() + "/";
        } else {
            //教师的处理 文件夹或文件打开
            if (!StringUtils.isEmpty(filePath)) {
                fileBaseUrl = filePath + "/";
            }
        }
        Map<String, Object> resultMap = new HashMap<>(2);
        resultMap.put("count", 0);
        resultMap.put("reportList", null);
        Integer count = aliYunOssUtil.listFileCount(sid + GlobalConst.EXPREPORT_FILE + fileBaseUrl);
        if (count != 0) {
            //进行返回内容的更换
            resultMap.put("count", count);
            List<String> fileList = aliYunOssUtil.listPageFile(sid + GlobalConst.EXPREPORT_FILE + fileBaseUrl, page, limit);
            List<FileType> labList = new ArrayList<>();
            for (String path : fileList) {
                String type = path.substring(path.lastIndexOf(".") + 1);
                String name = path.substring(path.lastIndexOf("/") + 1).split("\\.")[0];
                //文件夹路径
                String dirPath = "";
                //文件夹类型检测
                if (StringUtils.isEmpty(name)) {
                    type = "directory";
                    String[] split = path.split("/");
                    if (split.length > 0) {
                        name = split[split.length - 1];
                        dirPath = split[split.length - 1];
                    }

                }
                labList.add(new FileType(type, name, path, path, dirPath));
            }
            resultMap.put("reportList", labList);
        }
        return resultMap;
    }


    @Override
    public boolean reportDelete(String fileName, Authentication authentication, String fileType, Integer sid) {
        if (StringUtils.isEmpty(fileName) || StringUtils.isEmpty(fileType)) {
            return false;
        }
        return aliYunOssUtil.deleteFile(sid + GlobalConst.EXPREPORT_FILE + authentication.getName() + "/" + fileName + "." + fileType);
    }
}
